package cn.edu.sdjzu.xg.kcsj.security;

import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.util.JdbcHelper;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public class ResourceService {
  private static ResourceDao resourceDao = ResourceDao.getInstance();
  private static ResourceService resourceService = new ResourceService();

  public static ResourceService getInstance() {
    return resourceService;
  }

  // 查找全部院系
  public Collection<Resource> findAll(List conditionList, Pagination pagination) throws SQLException {
    Connection connection = JdbcHelper.getConn();
    //将自动提交设为false,开始事务
    connection.setAutoCommit(false);
    Collection<Resource> resources = new TreeSet<Resource>();
    try {
      resources = resourceDao.findAll(conditionList, pagination, connection);
      //提交事务
      connection.commit();
    } catch (SQLException e) {
      //回滚
      connection.rollback();
    } finally {
      //将自动提交设为true，结束事务
      connection.setAutoCommit(true);
      JdbcHelper.close(connection);
    }
    return resources;
  }

  public Collection<Resource> findAll() throws SQLException {
    Connection connection = JdbcHelper.getConn();
    //将自动提交设为false,开始事务
    connection.setAutoCommit(false);
    Collection<Resource> resources = new TreeSet<Resource>();
    try {
      resources = resourceDao.findAll();
      //提交事务
      connection.commit();
    } catch (SQLException e) {
      //回滚
      connection.rollback();
    } finally {
      //将自动提交设为true，结束事务
      connection.setAutoCommit(true);
      JdbcHelper.close(connection);
    }
    return resources;
  }

  // 按id查找院系
  public Resource find(Integer id) throws SQLException {
    Connection connection = JdbcHelper.getConn();
    //将自动提交设为false,开始事务
    connection.setAutoCommit(false);
    Resource resource = null;
    try {
      resource = resourceDao.find(id, connection);
      //提交事务
      connection.commit();
    } catch (SQLException e) {
      //回滚
      connection.rollback();
    } finally {
      //将自动提交设为true，结束事务
      connection.setAutoCommit(true);
      JdbcHelper.close(connection);
    }
    return resource;
  }

  // 更新一条
  public boolean update(Resource resource) throws SQLException {
    Connection connection = JdbcHelper.getConn();
    //将自动提交设为false,开始事务
    connection.setAutoCommit(false);
    Boolean updated = false;
    try {
      updated = resourceDao.update(resource, connection);
      //提交事务
      connection.commit();
    } catch (SQLException e) {
      //回滚
      connection.rollback();
    } finally {
      //将自动提交设为true，结束事务
      connection.setAutoCommit(true);
      JdbcHelper.close(connection);
    }
    return updated;
  }

  public boolean add(Resource resource) throws SQLException {
    Connection connection = JdbcHelper.getConn();
    //将自动提交设为false,开始事务
    connection.setAutoCommit(false);
    Boolean added = false;
    try {
      //增加
      added = resourceDao.add(resource, connection);
      //提交事务
      connection.commit();
    } catch (SQLException e) {
      //回滚
      connection.rollback();
    } finally {
      //将自动提交设为true，结束事务
      connection.setAutoCommit(true);
      JdbcHelper.close(connection);
    }
    return added;
  }

  // 删除
  public boolean delete(Integer id) throws SQLException, BysjException {
    Connection connection = JdbcHelper.getConn();
    //将自动提交设为false,开始事务
    connection.setAutoCommit(false);
    Boolean deleted = false;
    try {
      // 如果有子节点
      if (resourceDao.find(id, connection).isHaveChild()) {
        throw new BysjException("存在下属系，不能删除！");
      } else {
        deleted = resourceDao.delete(id, connection);
        //提交事务
        connection.commit();
      }
    } catch (SQLException e) {
      //回滚
      connection.rollback();
    } finally {
      //将自动提交设为true，结束事务
      connection.setAutoCommit(true);
      JdbcHelper.close(connection);
    }
    return deleted;
  }

}
